package ldh.im.fxbase.dao;

import ldh.im.fxbase.data.pojo.ImSensitiveWord;
import ldh.im.fxbase.data.pojo.ext.ImSensitiveWordExt;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class ImSensitiveWordDao extends BaseDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImSensitiveWordDao.class);

    private static final String COLLECT_COLUMNS = "id, word, status statusValue, createTime, updateTime";

    public void batchInsert(List<ImSensitiveWord> sensitiveWords) throws SQLException {
        if (sensitiveWords.size() < 1) return;
        DbUtil.transaction((queryRunner, connection) -> {
            ArrayListHandler h = new ArrayListHandler();
            Object[][] paras = sensitiveWords.stream().map(sensitiveWord-> new Object[] {sensitiveWord.getWord(),
                    valueEnum(sensitiveWord.getStatus()), sensitiveWord.getCreateTime(), sensitiveWord.getUpdateTime()}).toArray(Object[][]::new);
            String sql = String.format("insert into im_sensitive_word(word, status, createTime, updateTime) values(?, ?, ?, ?)");
            List<Object[]> ids = queryRunner.insertBatch(connection, sql, h, paras);
            for (int i=0; i<sensitiveWords.size(); i++) {
                sensitiveWords.get(i).setId((Long)ids.get(i)[0]);
            }

            return ids;
        });
    }

    public void batchUpdate(List<ImSensitiveWord> sensitiveWords) throws SQLException {
        if (sensitiveWords.size() < 1) return;
        DbUtil.transaction((queryRunner, connection) -> {
            ArrayListHandler h = new ArrayListHandler();
            Object[][] paras = sensitiveWords.stream().map(sensitiveWord-> new Object[] {sensitiveWord.getWord(), valueEnum(sensitiveWord.getStatus()), sensitiveWord.getUpdateTime(),
                    sensitiveWord.getWord()}).toArray(Object[][]::new);
            String sql = String.format("update im_sensitive_word set status = ?, updateTime = ? where word = ?");
            int[] ns = queryRunner.batch(connection, sql, paras);
            if (ns.length != sensitiveWords.size()) {
                throw new RuntimeException("更新im_sensitive_word失败， n =" + ns.length);
            }

            return ns.length;
        });
    }

    public ImSensitiveWord getByWord(String word) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_sensitive_word where word = ?", COLLECT_COLUMNS);
            LOGGER.debug("getByWord sql: {}", sql);
            ImSensitiveWordExt sensitiveWord = queryRunner.query(connection, sql, new BeanHandler<>(ImSensitiveWordExt.class), word);

            return sensitiveWord;
        });
    }

    public List<ImSensitiveWord> getAll() throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_sensitive_word", COLLECT_COLUMNS);
            LOGGER.debug("getAll sql: {}", sql);
            List<ImSensitiveWordExt> sensitiveWords = queryRunner.query(connection, sql, new BeanListHandler<>(ImSensitiveWordExt.class));

            return sensitiveWords.stream().collect(Collectors.toList());
        });
    }
}
